package m.permission.model;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import m.framework.utils.FileUtil;
import m.framework.utils.StringUtil;
import m.framework.utils.tree.TreeNode;
import m.framework.utils.tree.TreeUtil;
import m.framework.utils.xml.XmlUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 系统菜单,全部写在menus.xml里
 * @author ming
 * @date 2014年2月19日下午7:32:13
 */
public class Menu implements TreeNode {
	//菜单配置文件路径
	public static final String MENU_FILE ="/config/menus.xml";
	//根目录对应的菜单id
	public static final String ROOT_ID="0";
	//菜单id分隔符
	public static final String ID_SPLIT="-";
	private static Logger log = LoggerFactory.getLogger(Menu.class);
	private static List menus;
	
	private String id;
	//菜单名称
	private String menuName;
	//对应路径,如果为空说明是目录
	private String url;
	//上级菜单
	private Menu parent;
	private String parentId;
	private Integer displayNo;
	//下级菜单
	private List<Menu> children;
	/**
	 * 对应的显示图标,保存的名称为css文件里的样式
	 * @see /assets/css/main.css下的nav-xxx
	 * */
	private String ico;
	public static Logger getLog() {
		return log;
	}
	public static void setLog(Logger log) {
		Menu.log = log;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getMenuName() {
		return menuName;
	}
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public Menu getParent() {
		return parent;
	}
	public void setParent(Menu parent) {
		this.parent = parent;
	}
	public String getParentId() {
		return parentId;
	}
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
	public Integer getDisplayNo() {
		return displayNo;
	}
	public void setDisplayNo(Integer displayNo) {
		this.displayNo = displayNo;
	}
	public List<Menu> getChildren() {
		return children;
	}
	public void setChildren(List children) {
		this.children = children;
	}
	public String getText() {
		return menuName;
	}
	public void setText(String text) {
		this.menuName=text;
	}
	public void setParent(TreeNode parent) {
		this.parent=(Menu) parent;
	}
	
	public static List<Menu> getAll(){
		if(menus==null){
			String fileName=Menu.class.getResource(MENU_FILE).getFile();
			File file=new File(fileName);
			menus=new Menu().load(file);
		}
		return menus;
	}
	
	public static void reload(){
		String fileName=Menu.class.getResource(MENU_FILE).getFile();
		File file=new File(fileName);
		menus=new Menu().load(file);
	}
	
	
	public List load(File file) {
		String xml=FileUtil.readFile(file.getAbsolutePath(),FileUtil.utf8);
		Menu root=XmlUtil.toBean(xml, Menu.class);
		List<Menu> m=new ArrayList();
		addChildren(root, m,"",0);
		TreeUtil.createTree(m);
		return m;
	}
	
	private void fillMenu(Menu menu,String parentId,Integer displayNo){
		if(StringUtil.isEmpty(parentId)){
			menu.setId(ROOT_ID);
		}else{
			menu.setId(parentId+ID_SPLIT+displayNo);
		}
		menu.setParentId(parentId);
		menu.setDisplayNo(displayNo);
	}
	
	private void addChildren(Menu menu,List list,String parentId,int index){
		this.fillMenu(menu,parentId, index);
		log.info("新增菜单"+menu.getId()+"----------"+menu.getMenuName());
		list.add(menu);
		if(menu.getChildren()==null){
			return;
		}
		int displayNo=0;
		for(Object o:menu.getChildren()){
			addChildren((Menu) o,list,menu.getId(),displayNo);
			displayNo++;
		}
	}
	public String getIco() {
		return ico;
	}
	public void setIco(String ico) {
		this.ico = ico;
	}
	
	
}




